<?php

	/**
	 * Defines a class to be used for SQLITE databases
	 *
	 * @author Marcelo Bento da Rocha <marcelobentodarocha@gmail.com >
	 * @copyright Copyright (c) 2011, Marcelo Bento da Rocha
	 * @license http://opensource.org/licenses/bsd-license.php BSD License
	 * @package pop
	 * @subpackage drivers
	 */

	/**
	 * Defines a class to be used for SQLITE databases
	 *
	 * @package pop
	 * @subpackage drivers
	 */
	class PSQLiteDBDriver extends PGenericDBDriver
	{
		public $schema = '';
		public $separator = '';
		public $driver = 'sqlite';

		/**
		 * Class constructor
		 * @param $pdodb
		 * @return void
		 */
		public function __construct()
		{
		}

		public function newConnection($server,$dbpath,$user,$password)
		{
			if (file_exists($dbpath))
				POPDebug::log('Opening database file on '. $dbpath);
			else
				POPDebug::log('Creating database file on '. $dbpath);

			$conn = new PDO($this->driver.':'.$dbpath);
			return $conn;
		}
		
	/**
		 * Filter the field for update
		 * @param $value
		 * @param $type
		 * @param $defaultvalue
		 * @return string
		 */
		public function filterUpdate($value, $type, $defaultval = null)
		{
			if (is_null($value) || $value == "null" && is_null($defaultval))
				$value = "null";
				
			if (($type=="PText" || $type=="PVarchar" || $type=="PDate" || $type=="PTime" || $type=="PDatetime") && $value != "null")
				return "'".str_replace("'","''",($value))."'";
			else
				return parent::filterUpdate($value, $type, $defaultval);
		}
		
		/**
		 * Filter the field for insert
		 * @param $value
		 * @param $type
		 * @param $defaultvalue
		 * @return string
		 */
		public function filterInsert($value, $type, $defaultval = null)
		{
			if (is_null($value) || $value == "null" && is_null($defaultval))
				$value = "null";
				
			if (($type=="PText" || $type=="PVarchar" || $type=="PDate" || $type=="PTime" || $type=="PDatetime") && $value != "null")
				return "'".str_replace("'","''",($value))."'";
			else
				return parent::filterInsert($value, $type, $defaultval);
		}
		
		/**
		 * Filter the field for select
		 * @param $tablename
		 * @param field
		 * @param $key
		 * @param $type
		 * @return string
		 */
		public function filterSelect($tablename, $field, $key, $type)
		{
			if ($type == "PDate")
				return "date(".$tablename.".".$field.", 'YYYY-MM-DD') as ".$key;
			else if ($type == "PDatetime")
				return "datetime(".$tablename.".".$field.", 'YYYY-MM-DD HH:MM:SS.SSS') as ".$key;
			else if ($type == "PTime")
				return "time(".$tablename.".".$field.", 'HH:MI:SS.SSS') as ".$key;
			else
				return parent::filterSelect($tablename, $field, $key, $type);
		}

		/**
		 * Returns the last inserted itme id
		 * @param $pdodb
		 * @param $table
		 * @param $idname
		 * @return integer
		 */
		public function getLastId($pdodb, $table, $idname)
		{
			return POPDB::getConnection($pdodb)->lastInsertId();
		}

		/**
		 * Returns the required string for the createTable method
		 * @return string
		 */
		public function identityField()
		{
			return " integer not null ";
		}

		/**
		 * Creates the required schema to avoid errors on creating tables
		 * @param $schema
		 * @return void
		 */
		public function createSchema($pdodb, $schema)
		{
			//mysql doesn't work with schemas in a way that it's necessary
			//to create the schema, so, this method does nothing
		}

		/**
		 * Returns the required SQL for field creation
		 * @param $type
		 * @param $defaultval
		 * @param $size
		 * @return void
		 */
		public function fieldCreation($type, $defaultval, $size=null)
		{
			if ($type=="datetime" || $type=="date" || $type=="time")
				$sql_field = " text(23) ";
			else
				$sql_field = " ".$type." ";

			if ($type=="varchar")
				$sql_field .= "(".$size.") ";

			if (is_null($defaultval))
				$sql_field .= "default null";
			else
			{
				if ($defaultval && $type!="datetime" && $type!="date" && $type!="time" && $type!="varchar" && $type!="text")
					$sql_field .= "default ".$defaultval;
				else if ($type=="varchar")
					$sql_field .= "default '".$defaultval."'";
			}

			return $sql_field;
		}

		/**
		 * Alter table
		 * @return void
		 */
		public function alterTable()
		{
		}

		/**
		 * Drops a table
		 * @param $pdodb
		 * @param $schema
		 * @param $table
		 * @return void
		 */
		public function dropTable($pdodb, $schema, $table)
		{
			$sql = "drop table if exists ".$table.";\n";
			POPDB::getConnection($pdodb)->exec($sql);
			POPDebug::log($sql);
		}
	}

	/**
	 * Register the class for the driver
	 */
	POPDBDriverRegistry::addDriver("sqlite", "PSQLiteDBDriver");
?>